package repository

type engineParamService struct {
	engineParamMap map[string]interface{}
}

func (e *engineParamService) GetDefault(operateMode string) map[string]interface{} {
	value := e.engineParamMap[operateMode]
	if value == nil {
		return nil
	}

	return value.(map[string]interface{})
}

func (e *engineParamService) initialize() {
	exchange := make(map[string]interface{})
	exchange["encode"] = e.buildEncodeDefault()
	exchange["decode"] = e.buildDecodeDefault()
	e.engineParamMap["exchange"] = exchange

	publish := make(map[string]interface{})
	publish["encode"] = e.buildEncodeDefault()
	e.engineParamMap["publish"] = publish

	report := make(map[string]interface{})
	report["decode"] = e.buildDecodeDefault()
	e.engineParamMap["report"] = report

	include := make(map[string]interface{})
	include["include"] = e.buildIncludeDefault()
	e.engineParamMap["include"] = include

	keyHandler := make(map[string]interface{})
	keyHandler["decode"] = e.buildKeyHandlerDefault()
	e.engineParamMap["keyHandler"] = keyHandler

	splitHandler := make(map[string]interface{})
	splitHandler["decode"] = e.buildSplitHandlerDefault()
	e.engineParamMap["splitHandler"] = splitHandler

	respond := make(map[string]interface{})
	respond["encode"] = e.buildEncodeDefault()
	respond["decode"] = e.buildDecodeDefault()
	e.engineParamMap["respond"] = exchange
}

func (e *engineParamService) buildEncodeDefault() map[string]interface{} {
	code := "/* ----------------------------------------------------------------------------\n * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2020. All rights reserved.\n *\n *     This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n *     This program is distributed in the hope that it will be useful,\n *     but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n * GNU General Public License for more details.\n *\n *     You should have received a copy of the GNU General Public License\n * along with e program.  If not, see <https://www.gnu.org/licenses/>.\n * --------------------------------------------------------------------------- */\n\n/**\n * 编码器的入口函数\n * 全局参数：\n *     fox_edge_param：json string格式的设备参数的合并对象\n * 返回值：\n *   提供给通道的发送数据。根据不同的通道服务，它可能是HEX结构的文本，也可能是JSON结构的对象，请自行根据选定的通道服务进行确认\n */\nfunction encode() {\n    return encodeHex(fox_edge_param);\n}\n\n/**\n * 开发者自己写的编码函数1\n * 返回格式1：HEX格式的字符串\n *     比如串口通道，TCP通道、模拟通道，它们的输入/输出格式，就是HEX格式的文本\n */\nfunction encodeHex(param) {\n    return \"46 49 4E 53 00 00 00 1A 00 00 00 02 00 00 00 00 80 00 02 00 0A 00 00 71 00 FF 01 01 B1 00 0A 00 00 01\";\n}\n\n/**\n * 开发者自己写的编码函数2\n * 返回格式2：JSON格式的字符串\n *     比如S7通道、MQTT通道，它们的输入/输出格式，就是JSON格式的文本\n */\nfunction encodeJson(param) {\n    var object = JSON.parse(param);\n    return JSON.stringify(object);\n}"

	scriptMethod := make(map[string]interface{})
	scriptMethod["main"] = "encode"
	scriptMethod["code"] = code
	return scriptMethod
}

func (e *engineParamService) buildDecodeDefault() map[string]interface{} {
	code := "/* ----------------------------------------------------------------------------\n * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2020. All rights reserved.\n *\n *     This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n *     This program is distributed in the hope that it will be useful,\n *     but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n * GNU General Public License for more details.\n *\n *     You should have received a copy of the GNU General Public License\n * along with e program.  If not, see <https://www.gnu.org/licenses/>.\n * --------------------------------------------------------------------------- */\n\n/**\n * 编码器的入口函数\n * 全局参数：\n *     fox_edge_data：json string格式 或者 hex string格式的接收数据\n *     fox_edge_param：json string格式的设备参数的合并对象\n * 返回值：\n *   提供给通道的发送数据。根据不同的通道服务，它可能是HEX结构的文本，也可能是JSON结构的对象，请自行根据选定的通道服务进行确认\n */\nfunction decode() {\n    return decodeJson(fox_edge_data, fox_edge_param);\n}\n\n/**\n * 开发者自己写的解码函数1\n * 返回格式：JSON格式的字符串\n */\nfunction decodeJson(recv, param) {\n    var object = JSON.parse(param);\n    return JSON.stringify(object);\n}"

	scriptMethod := make(map[string]interface{})
	scriptMethod["main"] = "encode"
	scriptMethod["code"] = code
	return scriptMethod
}

func (e *engineParamService) buildKeyHandlerDefault() map[string]interface{} {
	code := "/* ----------------------------------------------------------------------------\n * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2020. All rights reserved.\n *\n *     This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n *     This program is distributed in the hope that it will be useful,\n *     but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n * GNU General Public License for more details.\n *\n *     You should have received a copy of the GNU General Public License\n * along with e program.  If not, see <https://www.gnu.org/licenses/>.\n * --------------------------------------------------------------------------- */\n\n/**\n * 函数说明：当前报文携带的设备身份ID\n * 输入参数：HEX格式的字符串，或者是TXT格式的字符串，（具体看通道服务的配置，给你输入的是哪种格式，默认是HEX格式），用于表达接收到的一个报文\n * 返回格式：当前设备身份信息的字符串\n * 备注信息：在TCP上传输的协议，它的报文会包身份信息，用于帮助识别是哪个设备主动送过来的数据。\n */\nfunction getServiceKey(message)\n{\n    // LORA WAN的报文，它实际上没有设备ID，此时使用LoRa Wan Device来标识\n    if(message.length() < 32){\n        return \"LoRa Wan Device\";\n    }\n\n    var deviceId = message.substring(8,23);\n    return  deviceId;\n}"

	scriptMethod := make(map[string]interface{})
	scriptMethod["main"] = "encode"
	scriptMethod["code"] = code
	return scriptMethod
}

func (e *engineParamService) buildSplitHandlerDefault() map[string]interface{} {
	code := "/* ----------------------------------------------------------------------------\n * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2020. All rights reserved.\n *\n *     This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n *     This program is distributed in the hope that it will be useful,\n *     but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n * GNU General Public License for more details.\n *\n *     You should have received a copy of the GNU General Public License\n * along with e program.  If not, see <https://www.gnu.org/licenses/>.\n * --------------------------------------------------------------------------- */\n\n/**\n * 函数说明：一组报文的最小长度\n * 返回格式：最小包长度\n * 备注信息：在TCP上传输的协议，它的报文会包括包头和包长度，用来帮助数据的分拆。\n *           包长度所在的位置通常是固定的，所以可以知道一段数据包括包头域和包长度域，至少包括多少字节。\n */\nfunction getHeaderLength()\n{\n    return 8;\n}\n\n\n/**\n * 函数说明：当前报文的具体长度\n * 输入参数：HEX格式的字符串，或者是TXT格式的字符串，（具体看通道服务的配置，给你输入的是哪种格式，默认是HEX格式），用于表达接收到的一个报文\n * 返回格式：当前报文的长度\n * 备注信息：在TCP上传输的协议，它的报文会包括包头和包长度，用来帮助数据的分拆。\n *           包长度所在的位置通常是固定的，所以可以知道一段数据包括包头域和包长度域，至少包括多少字节。\n */\nfunction getPackLength(message)\n{\n    var hex = message.substring(6,8);\n    return  parseInt(hex, 16) + 8;\n}\n\n/**\n * 函数说明：当前报文是否为非法包\n * 输入参数：HEX格式的字符串，或者是TXT格式的字符串，（具体看通道服务的配置，给你输入的是哪种格式，默认是HEX格式），用于表达接收到的一个报文\n * 返回格式：布尔值\n * 备注信息：在TCP上传输的协议，它的报文会包括包头和包长度，用来帮助数据的分拆。\n *           包头通常是固定的标识，如果不是这些标识，那么说明是非法包\n */\nfunction isInvalidPack(message)\n{\n    if (message.substring(0, 4) == '2424') {\n        return false;\n    }\n\n    return true;\n}\n\n"

	scriptMethod := make(map[string]interface{})
	scriptMethod["main"] = "encode"
	scriptMethod["code"] = code
	return scriptMethod
}

func (e *engineParamService) buildIncludeDefault() map[string]interface{} {
	code := "/* ----------------------------------------------------------------------------\n * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2020. All rights reserved.\n *\n *     This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n *     This program is distributed in the hope that it will be useful,\n *     but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n * GNU General Public License for more details.\n *\n *     You should have received a copy of the GNU General Public License\n * along with e program.  If not, see <https://www.gnu.org/licenses/>.\n * --------------------------------------------------------------------------- */\n\n/**\n * 用户自定义的函数：\n * 注意，保持设备类型中的唯一性，设备类型级别的Script引擎，会自动装载include类型的各个操作方法\n * 那么，其他真正的设备操作方法，就就可以引用它了\n * /\n\n\n\n/**\n * 用户自定义的函数1：注意，保持设备类型中的全局唯一性\n */\n/**\nfunction func1(message)\n{\n}\n */\n\n\n/**\n * 用户自定义的函数2：注意，保持设备类型中的全局唯一性\n */\n/**\n function func2(message)\n {\n}\n */"

	scriptMethod := make(map[string]interface{})
	scriptMethod["main"] = "encode"
	scriptMethod["code"] = code
	return scriptMethod
}
